package com.fingard.dsp.bank.directbank.sdebank01;

import com.fingard.FGBiz;
import com.fingard.constant.Format;
import com.fingard.db.JDBCHelper;
import com.fingard.dsp.bank.directbank.bankInterface.IElecBillQuery;
import com.fingard.dsp.bank.reqrespobj.elecbillquery.*;
import com.fingard.dsp.bank.reqrespobj.elecbillquery.detail.RetAtsB001OperBillQryDetail;
import com.fingard.dsp.bank.reqrespobj.elecbillquery.detail.RetAtsB005BillBackQryDetail;
import com.fingard.dsp.bank.reqrespobj.elecbillquery.detail.RetAtsB006BillStateQryDetail;
import com.fingard.dsp.bank.reqrespobj.elecbillquery.detail.RetAtsB007BillKzsrQryDetail;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.sql.Connection;
import java.util.Date;
import java.util.Map;

/**
 * @author Parker on 2020-01-19.
 * @version 1.0
 */
public class SDEBANK01ElecBill extends SDEBANK01Base implements IElecBillQuery {

    @Override
    public RetAtsB001OperBillQry D_OperBill_Query_Req(ReqB001OperBillQry query) throws Exception {
        RetAtsB001OperBillQry retAts = new RetAtsB001OperBillQry(query.reqHeader);
        String respCode="";
        String respInfo="";
        retAts.respCode = "0";
        retAts.writeBefore();
        while (query.readRecord()) {
            try {
                if ("1".equals(query.detail.queryType)) {
                    //查询申请类的票据  持票查询 ok
                    queryBillByApply(query, retAts);
                } else if ("2".equals(query.detail.queryType)) {
                    //查询签收类的票据   应答查询 ok
                    queryBillByB2EAcceptSignableQry(query, retAts);
                } else if ("3".equals(query.detail.queryType)) {
                    //查询可撤销的票据
                    queryBillByCancel(query, retAts);
                }
            } catch (Exception ex) {
                //ex.printStackTrace();
                retAts.respCode = "-1";
                retAts.respInfo = ex.getMessage();
            }
        }
        respCode=retAts.respCode;
        respInfo=retAts.respInfo;
        if(!"0".equals(respCode)){
            retAts = new RetAtsB001OperBillQry(query.reqHeader);
            retAts.respCode=respCode;
            retAts.respInfo=respInfo;
            retAts.writeBefore();
        }
        retAts.writeEnd();
        return retAts;
    }

    //查询可撤销的票据
    private void queryBillByCancel(ReqB001OperBillQry query, RetAtsB001OperBillQry retAts) throws Exception {
        //待撤销票据查询
        if ("1".equals(query.detail.busiType)) {
            queryBillByB2EAcceptableQry(query, retAts, "0003");
        } else {
            switch (Integer.valueOf(query.detail.busiType)) {
                case 2:
                    queryBillByB2EAcceptCancelableQry(query, retAts, "0001", "020001");
                    break;//承兑撤回
                case 3:
                    queryBillByB2EAcceptCancelableQry(query, retAts, "0002", "030001");
                    break;//收票撤回
                case 4:
                    queryBillByB2EAcceptCancelableQry(query, retAts, "0004", "100001");
                    break;//背书撤回
                case 5:
                    queryBillByB2EAcceptCancelableQry(query, retAts, "0006", "180001");
                    break;//质押撤回
                case 6:
                    queryBillByB2EAcceptCancelableQry(query, retAts, "0007", "190001");
                    break;//质押解除撤回
                case 10:
                    queryBillByB2EAcceptCancelableQry(query, retAts, "0005", "110101");
                    queryBillByB2EAcceptCancelableQry(query, retAts, "0005", "110201");
                    break;//贴现撤回
                case 12:
                    if ("1".equals(query.detail.overdueFlag)) {
                        queryBillByB2EAcceptCancelableQry(query, retAts, "0008", "200001");
                    } else {
                        queryBillByB2EAcceptCancelableQry(query, retAts, "0008", "210001");//逾期提示付款待签收
                    }
                    break;//提示付款撤回
                case 13:
                    if ("1".equals(query.detail.recourseType)) {
                        queryBillByB2EAcceptCancelableQry(query, retAts, "0009", "220707");
                    } else {
                        queryBillByB2EAcceptCancelableQry(query, retAts, "0009", "220607");//拒付追索待清偿
                    }
                    break;//追索撤回
                case 14:
                    queryBillByB2EAcceptCancelableQry(query, retAts, "0010", "230601");
                    queryBillByB2EAcceptCancelableQry(query, retAts, "0010", "230701");
                    break;//非拒付追索同意清偿待签收
                default:
                    throw new Exception("未知的撤回业务类型：" + query.detail.busiType);
            }
        }
    }

    private void queryBillByB2EAcceptCancelableQry(ReqB001OperBillQry query, RetAtsB001OperBillQry retAts, String queryTp, String drftStat) throws Exception {
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //赋值通用参数
        SDEBANK01Header bankHeader = new SDEBANK01Header();
        bankHeader.TrsCode = "B2EAcceptCancelableQry";//银行交易码
        bankHeader.ERPJnlNo = getQryTransID();//生成交易流水号
        bankHeader.ERPTrsTimestamp = query.reqHeader.transTime;//指令请求时间
        //写入SDEBANK01通用的头部节点
        writeCommonHeader(xmlWriter, bankHeader);
        //写入发送给银行报文体
        xmlWriter.writeStartElement("Body");
        xmlWriter.writeElementString("EnterpriseAccount", query.detail.actNum);//账号
        if ("1".equals(query.detail.billType)) {
            xmlWriter.writeElementString("DraftType", "AC01");//AC01银承
        } else {
            xmlWriter.writeElementString("DraftType", "AC02");//AC02商承
        }
        xmlWriter.writeElementString("DraftNb", query.detail.billNo);//电子票据号码
        xmlWriter.writeElementString("DrftStat", drftStat);//票据状态
        xmlWriter.writeElementString("QueryTp", queryTp);//电票交易类型
        xmlWriter.writeElementString("IsseDtBegin", "");//查询起始日期
        xmlWriter.writeElementString("IsseDtEnd", "");//查询结束日期
        xmlWriter.writeElementString("IsseAmtLower", "");//票据金额1
        xmlWriter.writeElementString("IsseAmtLimit", "");//票据金额2
        xmlWriter.writeElementString("DueDtBegin", "");//票据到期日开始
        xmlWriter.writeElementString("DueDtEnd", "");//票据到期日结束
        xmlWriter.writeEndDocument();

        // 发送给银行获得应答报文
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankHeader);

        if (tmpStrRet[0].length() == 0) {// 成功响应
            StringBuilder tmpSbMsg = new StringBuilder();//存提示信息
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);// 把银行返回报文转成XmlTextReader格式
            if (tmpBkReader != null) {// 返回报文的内容不为空
                String IBSReturnCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                String IBSReturnMsg = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                if ("000000".equals(IBSReturnCode)) {
                    while (tmpBkReader.read("/Message/Body/List/Map")) {
                        String applyNum = tmpBkReader.readedXElement.getSingleText("BkAcctNo7");
                        if (query.detail.actNum.equals(applyNum)) {
                            RetAtsB001OperBillQryDetail detail = new RetAtsB001OperBillQryDetail();
                            detail.billNo = tmpBkReader.readedXElement.getSingleText("draftNb");
                            detail.billAmt = tmpBkReader.readedXElement.getSingleText("isseAmt");
                            detail.billType = query.detail.billType;
                            detail.issDate = DateHelper.convertD8toD10(tmpBkReader.readedXElement.getSingleText("isseDt"));
                            detail.dueDate = DateHelper.convertD8toD10(tmpBkReader.readedXElement.getSingleText("dueDt"));
                            detail.transFlag = getTransFlag(tmpBkReader.readedXElement.getSingleText("banEndrsmtMk"));
                            detail.drawAct = tmpBkReader.readedXElement.getSingleText("drwrAcctId");
                            detail.drawName = tmpBkReader.readedXElement.getSingleText("drwrNm");
                            detail.drawBnkCode = tmpBkReader.readedXElement.getSingleText("drwrAcctSvcr");
                            detail.accpName = tmpBkReader.readedXElement.getSingleText("accptrNm");
                            detail.accpAct = tmpBkReader.readedXElement.getSingleText("accptrAcNo");
                            detail.accpBnkCode = tmpBkReader.readedXElement.getSingleText("accptrSvcr");
                            detail.payeeAct = tmpBkReader.readedXElement.getSingleText("pyeeAcNo");
                            detail.payeeName = tmpBkReader.readedXElement.getSingleText("pyeeNm");
                            detail.payeeBnkCode = tmpBkReader.readedXElement.getSingleText("pyeeSvcr");
                            //备注
                            detail.memo=tmpBkReader.readedXElement.getSingleText("draftReRemrk");
                            //交易合同号码
                            detail.contractNo = tmpBkReader.readedXElement.getSingleText("contractNb");
                            //发票号码
                            detail.invoiceNo = tmpBkReader.readedXElement.getSingleText("invoiceNb");
                            String discOrResType=tmpBkReader.readedXElement.getSingleText("discOrResType");
                            if("10".equals(query.detail.busiType) || "11".equals(query.detail.busiType)){
                                //贴现种类 1-买断式 2-回购式
                                detail.discType = getDiscType(discOrResType);
                            }else if("13".equals(query.detail.busiType)){
                                //追索类型 1-拒付追索 2-非拒付追索
                                detail.recourseType = getRecourseType(discOrResType);
                            }
                            //清算标记 1-线上清算 2-线下清算
                            detail.payType = getPayType(tmpBkReader.readedXElement.getSingleText("sttlmMk"));
                            //回购开放日期
                            detail.repOpenDate = "";
                            //回购截止日期
                            detail.repDueDate = "";
                            //贴现利率
                            detail.discRate = tmpBkReader.readedXElement.getSingleText("intrstRate");
                            //回购利率
                            detail.repRate = "";
                            //入账账号
                            detail.actedAct = "";
                            //入账行号
                            detail.actedBnkCode = tmpBkReader.readedXElement.getSingleTextNotNull("accnInfSvcr");
                            if("12".equals(query.detail.busiType)){
                                String drftHldRsn=tmpBkReader.readedXElement.getSingleText("drftHldRsn");//逾期原因说明
                                if(StringHelper.hasAnyChar(drftHldRsn)){
                                    //逾期标志 1-正常 2-逾期
                                    detail.overDueFlag = "2";
                                    //逾期原因说明
                                    detail.overDueRea = drftHldRsn;
                                }else {
                                    detail.overDueFlag = "1";
                                }
                            }
                            //追索金额
                            detail.recourseAmt = "";
                            //追索理由代码（非拒付时） 1-承兑人被依法宣告破产 2-承兑人因违法被责令终止活动
                            detail.recourseCode = tmpBkReader.readedXElement.getSingleText("rcrsRsnCd");

                            detail.billStatus = tmpBkReader.readedXElement.getSingleText("drftStat");
                            detail.busiNo = tmpBkReader.readedXElement.getSingleText("EcdsDate") + "_" +
                                    tmpBkReader.readedXElement.getSingleText("EcdsSeq");
                            retAts.writeDetail(detail);
                        }
                    }
                } else if("999999".equals(IBSReturnCode) && "无结果查询的记录".equals(IBSReturnMsg.trim())){
                    retAts.respCode = "0";
                    retAts.respInfo = IBSReturnMsg.trim();
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = StringHelper.isNullOrEmpty(IBSReturnMsg)?"银企直联通讯机校验错误，或者通讯失败！":IBSReturnMsg;
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpSbMsg.toString();
            }
        } else {
            retAts.respCode = "-1";
            retAts.respInfo = tmpStrRet[1];
        }
    }

    private void queryBillByB2EAcceptSignableQry(ReqB001OperBillQry query, RetAtsB001OperBillQry retAts) throws Exception {
        //赋值通用参数
        SDEBANK01Header bankHeader = new SDEBANK01Header();
        bankHeader.TrsCode = "B2EAcceptSignableQry";//银行交易码
        bankHeader.ERPTrsTimestamp = query.reqHeader.transTime;//指令请求时间
        bankHeader.ERPJnlNo = getQryTransID();//生成交易流水号
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //写入SDEBANK01通用的头部节点
        writeCommonHeader(xmlWriter, bankHeader);
        //写入发送给银行报文体
        xmlWriter.writeStartElement("Body");
        String busiType;
        /// 业务类型 1-出票 2-承兑 3-收票 4-背书转让 5-质押 6-解质押 7-出票保证 8-承兑保证
        // 9-背书保证 10-贴现 11-回购式贴现赎回 12-提示付款 13-追索 14-同意清偿
        switch (Integer.valueOf(query.detail.busiType)) {
            case 2: busiType = "4";break;//承兑应答 ok
            case 3: busiType = "0";break;//收款人应答
            case 4: busiType = "5";break;//背书待签收
            case 5: busiType = "9";break;//质押应答
            case 6: busiType = "10";break;//质押解除应答
            case 7: busiType = "1";break;//出票保证
            case 8: busiType = "1";break;//承兑保证
            case 9: busiType = "1";break;//背书保证
            //case 10: busiType = "0001";break;
            case 11: busiType = "11";break;//回购式贴现赎回
            case 12: busiType = "6";break;//付款应答
            case 13: busiType = "0002";break;//追索应答
            case 14: busiType = "8";break;//同意清偿
            default: busiType = "0001";
        }
        xmlWriter.writeElementString("EnterpriseAccount", query.detail.actNum);//账号
        xmlWriter.writeElementString("DraftType", getBillType(query.detail.billType));//AC01银承
        xmlWriter.writeElementString("IsseDtBegin", "");//交易日期
        xmlWriter.writeElementString("QueryTp", busiType);//应答类型
        xmlWriter.writeEndDocument();
        // 发送给银行获得应答报文
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankHeader);
        if (tmpStrRet[0].length() == 0) {// 成功响应
            StringBuilder tmpSbMsg = new StringBuilder();//存提示信息
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);// 把银行返回报文转成XmlTextReader格式
            if (tmpBkReader != null) {// 返回报文的内容不为空
                String IBSReturnCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                String IBSReturnMsg = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                if ("000000".equals(IBSReturnCode)) {
                    while (tmpBkReader.read("/Message/Body/List/Map")) {
                        String signAct = tmpBkReader.readedXElement.getSingleText("SignerAcct");
                        if (StringHelper.hasAnyChar(signAct) && signAct.contains(query.detail.actNum)) {
                            //应答类型 Char(4) F
//                                          0:收款人应答
//                                          1:出票保证人应答
//                                          2:承兑保证人应答
//                                          3:背书保证人应答（1，2，3目前根据人行接口无法区分具体何种保证，返回网银统一为1）
//                                          4:承兑应答
//                                          5:背书转让应答
//                                          6:付款应答
//                                          7：追索应答（被追索人收到追索人发起的追索申请后，可以发起同意清偿或不予处理）
//                                          8:同意清偿应答（追索人收到被追索人发起的清偿申请后，做签收或驳回）
//                                          9.质押应答（只有商承）
//                                          10:质押解除应答
//                                          11:贴现赎回应答
                            String bktype2 = tmpBkReader.readedXElement.getSingleText("queryTp");
                            //if (!checkBkType(Integer.parseInt(query.detail.busiType), query.detail.queryType, bktype2, query.detail.billType)) {
                            //    continue;
                            //}
                            RetAtsB001OperBillQryDetail detail = new RetAtsB001OperBillQryDetail();
//                          电子票据号码
                            detail.billNo = tmpBkReader.readedXElement.getSingleText("draftNb");
                            //票据类型
                            detail.billType = query.detail.billType;
                            //出票日期 Char(8) F 　
                            detail.issDate = DateHelper.convertD8toD10(tmpBkReader.readedXElement.getSingleText("isseDt"));
                            //汇票到期日 Char(8) F 　
                            detail.dueDate = DateHelper.convertD8toD10(tmpBkReader.readedXElement.getSingleText("dueDt"));
//                          出票金额 Decimal(18,2) F 　
                            detail.billAmt = tmpBkReader.readedXElement.getSingleText("isseAmt");
//                          不得转让标识 Char(4) F 票据的属性
                            detail.transFlag = getTransFlag(tmpBkReader.readedXElement.getSingleText("banMk"));
                            //承兑无条件支付委托 1-无条件支付 0-非条件支付
                            detail.mustPay = "1";
                            //出票人名称 varChar(120) F 　
                            detail.drawName = tmpBkReader.readedXElement.getSingleText("drwrNm");
                            //出票人开户行 varChar(12) F 　
                            detail.drawBnkCode = tmpBkReader.readedXElement.getSingleText("drwrAcctSvcr");
                            //出票人账号 varChar(32) F 　
                            detail.drawAct = tmpBkReader.readedXElement.getSingleText("drwrAcctId");
                            //承兑人名称 varChar(120) F 　
                            detail.accpName = tmpBkReader.readedXElement.getSingleText("accptrNm");
                            //承兑人开户行 varChar(12) F 　
                            detail.accpBnkCode = tmpBkReader.readedXElement.getSingleText("accptrSvcr");
                            //收款人名称 varChar(120) F 　
                            detail.payeeName = tmpBkReader.readedXElement.getSingleText("payeeNm");
                            //收款人账号 varChar(32) F 　
                            detail.payeeAct = tmpBkReader.readedXElement.getSingleText("payeeAcNo");
                            //收款人开户行 varChar(12) F 　
                            detail.payeeBnkCode = tmpBkReader.readedXElement.getSingleText("payeeSvcr");
                            //交易明细流水：原ECDS平台日期+原ECDS平台流水
                            detail.busiNo = tmpBkReader.readedXElement.getSingleText("EcdsDate") + "_" + tmpBkReader.readedXElement.getSingleText("EcdsSeq");
                            //申请人名称 varChar(120) T 　
                            detail.applyName = tmpBkReader.readedXElement.getSingleText("rqstrNm");
                            //申请人账号 varChar(32) T 　
                            detail.applyAct = tmpBkReader.readedXElement.getSingleText("rqstrAcct");
                            //申请人开户行行号 varChar(12) T 　
                            detail.applyBnkCode = tmpBkReader.readedXElement.getSingleText("rqstrAcct");
                            if ("0003".equals(busiType)) {
                                if (StringHelper.isNullOrEmpty(detail.applyAct)) {
                                    detail.applyAct = detail.drawAct;
                                }
                                if (StringHelper.isNullOrEmpty(detail.applyName)) {
                                    detail.applyName = detail.drawName;
                                }
                                if (StringHelper.isNullOrEmpty(detail.applyBnkCode)) {
                                    detail.applyBnkCode = detail.drawBnkCode;
                                }
                            }
//                          签收人名称 varChar(120) F 当应答类型为8时返回被追索人
                            detail.receName = tmpBkReader.readedXElement.getSingleText("SignerNm");
//                          签收人账号 varChar(32) F
                            detail.receAct = tmpBkReader.readedXElement.getSingleText("SignerAcct");
//                          签收人开户行行号 varChar(12) F
                            detail.receBnkCode = tmpBkReader.readedXElement.getSingleText("SignerAcctSvcr");
                            //申请人备注 varChar(512) T 　
                            String memo=tmpBkReader.readedXElement.getSingleText("rqstrReRemrk");
                            if(memo.contains(splitToString)){
                                detail.memo = memo.substring(0,memo.indexOf(splitToString));
                            }else {
                                detail.memo = memo;
                            }
                            //逾期原因说明
                            detail.overDueRea = tmpBkReader.readedXElement.getSingleText("Bk60Name8");
                            if ("6".equals(bktype2)) {
                                if (StringHelper.hasAnyChar(detail.overDueRea)) {
                                    detail.overDueFlag = "2";
                                } else {
                                    detail.overDueFlag = "1";
                                }
                            }
                            //追索类型 1-拒付追索 2-非拒付追索
                            detail.recourseType = "";
                            //追索金额
                            String recoAmt = tmpBkReader.readedXElement.getSingleText("BkAmt2");
                            if (StringHelper.hasAnyChar(recoAmt)) {
                                detail.recourseAmt = recoAmt;
                            }
                            // 票据状态 Char(6) F 　
                            detail.billStatus = tmpBkReader.readedXElement.getSingleText("drftStat");
//                           清偿金额 Decimal(18,2) T 在应答类型为7,8时，必须返回
                            detail.recourseAmt = tmpBkReader.readedXElement.getSingleText("Amount");
                            retAts.writeDetail(detail);
                        }
                    }
                } else if("999999".equals(IBSReturnCode) && "无结果查询的记录".equals(IBSReturnMsg)){
                    retAts.respCode = "0";
                    retAts.respInfo = IBSReturnMsg.trim();
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = StringHelper.isNullOrEmpty(IBSReturnMsg)?"银企直联通讯机校验错误，或者通讯失败！":IBSReturnMsg;
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpSbMsg.toString();
            }
        } else {
            retAts.respCode = "-1";
            retAts.respInfo = tmpStrRet[1];
        }
    }

    @Override
    public RetAtsB002BillTransQry D_B002_BillTrans_Query(ReqB002BillTransQry query) throws Exception {
        return null;
    }

    //正面信息查询 ok
    @Override
    public RetAtsB004BillFrontQry D_B004_BillFront_Query(ReqB004BillFrontQry query) throws Exception {
        RetAtsB004BillFrontQry retAts = new RetAtsB004BillFrontQry(query.reqHeader);
        retAts.writeStart();
        while (query.readRecord()) {
            try {
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //赋值通用参数
                SDEBANK01Header bankHeader = new SDEBANK01Header();
                bankHeader.TrsCode = "B2EEdraftFaceQueryByTrs";//银行交易码
                bankHeader.ERPJnlNo = getQryTransID();//生成交易流水号
                bankHeader.ERPTrsTimestamp = query.reqHeader.transTime;//指令请求时间
                //写入SDEBANK01通用的头部节点
                writeCommonHeader(xmlWriter, bankHeader);
                //写入发送给银行报文体
                xmlWriter.writeStartElement("Body");
                xmlWriter.writeElementString("DraftNb", query.detail.billNo);//电子票据号码
                xmlWriter.writeElementString("EnterpriseAccount", query.detail.actNum);//账号
                xmlWriter.writeElementString("QueryTp", "");//查看类型
                xmlWriter.writeEndDocument();

                // 发送给银行获得应答报文
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankHeader);

                if (tmpStrRet[0].length() == 0) {// 成功响应
                    StringBuilder tmpSbMsg = new StringBuilder();//存提示信息
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);// 把银行返回报文转成XmlTextReader格式
                    if (tmpBkReader != null) {// 返回报文的内容不为空
                        String IBSReturnCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                        String IBSReturnMsg = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                        if ("000000".equals(IBSReturnCode)) {
                            retAts.respCode = "0";
                            retAts.respInfo = "";
                            retAts.billNo = tmpBkReader.getFirstText("/Message/Body/DraftNb");//电子票据号码
                            retAts.billType = getBillType(tmpBkReader.getFirstText("/Message/Body/DraftType"));
                            //出票日期
                            retAts.issDate = tmpBkReader.getFirstText("/Message/Body/IsseDt");
                            //到期日
                            retAts.dueDate = tmpBkReader.getFirstText("/Message/Body/DueDt");
                            //票据金额
                            retAts.billAmt = tmpBkReader.getFirstText("/Message/Body/IsseAmt");
                            //转让标记
                            retAts.transFlag = getTransFlag(tmpBkReader.getFirstText("/Message/Body/EndrsmtMk"));
                            //出票人
                            retAts.drawName = tmpBkReader.getFirstText("/Message/Body/DrwrNm");
                            retAts.drawCode = tmpBkReader.getFirstText("/Message/Body/DrwrCmonID");
                            retAts.drawAct = tmpBkReader.getFirstText("/Message/Body/DrwrAcctId");
                            retAts.drawBnkCode = tmpBkReader.getFirstText("/Message/Body/DrwrSvcr");
                            retAts.creditLv = tmpBkReader.getFirstText("/Message/Body/DrwrCdtRatgs");
                            retAts.gradeAgent = tmpBkReader.getFirstText("/Message/Body/DrwrCdtRatgAgcy");
                            retAts.gradeDueDate = tmpBkReader.getFirstText("/Message/Body/DrwrCdtRatgDueDt");
                            retAts.drGuarName = tmpBkReader.getFirstText("/Message/Body/List/DrwrguarntrNm");//出票保证人名称
                            retAts.drGuarAddr = tmpBkReader.getFirstText("/Message/Body/List/DrwrguarntrAddr");//出票保证人地址
                            retAts.drGuarDate = tmpBkReader.getFirstText("/Message/Body/List/DrwrguarntrDt");//出票保证日期
                            //承兑人
                            retAts.accpName = tmpBkReader.getFirstText("/Message/Body/AccptrNm");
                            retAts.accpAct = tmpBkReader.getFirstText("/Message/Body/AccptrAcct");
                            retAts.accpBnkName = tmpBkReader.getFirstText("/Message/Body/AccptrNmSvcr");
                            retAts.accpBnkCode = tmpBkReader.getFirstText("/Message/Body/AccptrAcctSvcr");
                            retAts.accpCredit = tmpBkReader.getFirstText("/Message/Body/AccptrCdtRatgs");
                            retAts.accpGrade = tmpBkReader.getFirstText("/Message/Body/AccptrCdtRatgAgcy");
                            retAts.accpGradeDue = tmpBkReader.getFirstText("/Message/Body/AccptrCdtRatgDueDt");
                            retAts.apGuarName = tmpBkReader.getFirstText("/Message/Body/List2/AccptrguarntrNm");//承兑保证人名称
                            retAts.apGuarAddr = tmpBkReader.getFirstText("/Message/Body/List2/AccptrguarntrAddr");//承兑保证人地址
                            retAts.apGuarDate = tmpBkReader.getFirstText("/Message/Body/List2/AccptrguarntrDt");//承兑保证日期
                            //收款人
                            retAts.payeeName = tmpBkReader.getFirstText("/Message/Body/PyeeNm");
                            retAts.payeeAct = tmpBkReader.getFirstText("/Message/Body/PyeeAcct");
                            retAts.payeeBnkCode = tmpBkReader.getFirstText("/Message/Body/PyeeAcctSvcr");
                            //票据状态
                            retAts.billState = tmpBkReader.getFirstText("/Message/Body/DrftStat");
                            //承兑日
                            retAts.accpDate = tmpBkReader.getFirstText("/Message/Body/AccptrDt");
                            //合同文本编号
                            retAts.contractNo = tmpBkReader.getFirstText("/Message/Body/CtrctNb");
                        } else {
                            retAts.respCode = "-1";
                            retAts.respInfo = StringHelper.isNullOrEmpty(IBSReturnMsg)?"银企直联通讯机校验错误，或者通讯失败！":IBSReturnMsg;
                        }
                    } else {
                        retAts.respCode = "-1";
                        retAts.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpStrRet[1];
                }
            } catch (Exception e) {
                retAts.respCode = "-1";
                retAts.respInfo = e.getMessage();
                e.printStackTrace();
            }
            retAts.writeRetDetail();
        }
        retAts.writeEnd();
        return retAts;
    }

    //票据背面查询 ok
    @Override
    public RetAtsB005BillBackQry D_B005_BillBack_Query(ReqB005BillBackQry query) throws Exception {
        RetAtsB005BillBackQry retAts = new RetAtsB005BillBackQry(query.reqHeader);
        retAts.writeStart();
        String respCode="";
        String respInfo="";
        while (query.readRecord()) {
            try {
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //赋值通用参数
                SDEBANK01Header bankHeader = new SDEBANK01Header();
                bankHeader.TrsCode = "B2EEdraftAllInfoQueryByNo";//银行交易码
                bankHeader.ERPJnlNo = getQryTransID();//生成交易流水号
                bankHeader.ERPTrsTimestamp = query.reqHeader.transTime;//指令请求时间
                //写入SDEBANK01通用的头部节点
                writeCommonHeader(xmlWriter, bankHeader);
                //写入发送给银行报文体
                xmlWriter.writeStartElement("Body");
                xmlWriter.writeStartElement("DraftNbList");
                xmlWriter.writeElementString("DraftNb", query.detail.billNo);//电子票据号码
                xmlWriter.writeEndDocument();
                // 发送给银行获得应答报文
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankHeader);
                if (tmpStrRet[0].length() == 0) {// 成功响应
                    StringBuilder tmpSbMsg = new StringBuilder();//存提示信息
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);// 把银行返回报文转成XmlTextReader格式
                    if (tmpBkReader != null) {// 返回报文的内容不为空
                        String IBSReturnCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                        String IBSReturnMsg = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                        if ("000000".equals(IBSReturnCode)) {
                            String billNo = tmpBkReader.getFirstText("/Message/Body/ResultList/Map/DraftNb");
                            //背书转让标识 1-可再转让 0-不得转让
                            String transFlag= getTransFlag(tmpBkReader.getFirstText("/Message/Body/ResultList/Map/EndrsmtMk"));
                            while (tmpBkReader.read("/Message/Body/ResultList/Map/List3/Map")){
                                RetAtsB005BillBackQryDetail detail=new RetAtsB005BillBackQryDetail();
                                detail.respCode="0";
                                detail.respInfo="";
                                //电子票据号码
                                detail.billNo = billNo;
                                // 00:出票信息01（提示承兑）02（提示出票）03（背书）04（贴现）05（贴现赎回）06（转贴现）07（转贴现赎回）08（再贴现）
                                // 09（再贴现赎回10（央行卖票）11（保证）12（质押）13（质押解除）14（提示付款）15（逾期提示付款）16（追索）17（追偿应答）
                                String transCode="";
                                String issueType=tmpBkReader.readedXElement.getSingleTextNotNull("IssueType");
                                int issueTypeIntValue=Integer.parseInt(issueType);
                                switch (issueTypeIntValue){
                                    case 0:transCode="1";break;//1 出票
                                    case 1:transCode="2";break;//2  承兑
                                    case 2:transCode="3";break;//3  收票
                                    case 3:transCode="10";break;//10  背书转让
                                    case 4:transCode="11";break;//11  贴现
                                    case 5:transCode="12";break;//12  回购式贴现赎回
                                    case 6:transCode="13";break;//13  转贴现
                                    case 7:transCode="14";break;//14  回购式转贴现赎回
                                    case 8:transCode="15";break;//15  再贴现
                                    case 9:transCode="16";break;//16  回购式再贴现赎回
                                    case 10:transCode="25";break;//25  央行卖出商业汇票
                                    case 11:transCode="17";break;//17  保证
                                    case 12:transCode="18";break;//18  质押
                                    case 13:transCode="19";break;//19  质押解除
                                    case 14:transCode="20";break;//20  提示付款
                                    case 15:transCode="21";break;//21  逾期提示付款
                                    case 16:transCode="22";break;//22  追索
                                    case 17:transCode="23";break;//23  追索同意清偿
                                    default:throw new Exception("票据业务类型匹配失败");
                                }
                                detail.transCode= transCode;
                                String signDate=tmpBkReader.readedXElement.getSingleTextNotNull("SignDate");
                                if(issueTypeIntValue>=1 && issueTypeIntValue<=13){
                                    //操作日期
                                    detail.respDate=signDate;
                                }else if(issueTypeIntValue>=14 && issueTypeIntValue<=17){
                                    //申请日期
                                    detail.applyDate=signDate;
                                }
                                String flagType=tmpBkReader.readedXElement.getSingleTextNotNull("FlagType");
                                if(issueTypeIntValue>=3 && issueTypeIntValue<=10){
                                    //不得转让标志
                                    detail.transFlag =getTransFlag(flagType);
                                }else if(issueTypeIntValue==16 || issueTypeIntValue==17){
                                    //1-拒付追索 2-非拒付追索
                                    detail.recourseType=getRecourseType(flagType);
                                }
                                if(StringHelper.isNullOrEmpty(detail.transFlag)){
                                    detail.transFlag=transFlag;
                                }
                                String openDate=tmpBkReader.readedXElement.getSingleTextNotNull("OpenDate");
                                String endDate=tmpBkReader.readedXElement.getSingleTextNotNull("EndDate");
                                if(issueTypeIntValue==4 || issueTypeIntValue==6 || issueTypeIntValue==8){
                                    //回购开放日
                                    detail.repOpenDate=openDate;
                                    //回购截止日期
                                    detail.repDueDate=endDate;
                                }else if(issueTypeIntValue>=14 && issueTypeIntValue<=17){
                                    //操作日期
                                    detail.respDate=openDate;
                                }
                                String drwrNm=tmpBkReader.readedXElement.getSingleTextNotNull("DrwrNm");
                                if(issueTypeIntValue<3){
                                    //接收人
                                    detail.receName=drwrNm;
                                }else {
                                    //申请人
                                    detail.applyName= drwrNm;
                                }
                                String cdtRatgAgcy=tmpBkReader.readedXElement.getSingleTextNotNull("CdtRatgAgcy");
                                if(issueTypeIntValue>=3 && issueTypeIntValue<=17){
                                    //接收人
                                    detail.receName= cdtRatgAgcy;
                                }
                                String remark=tmpBkReader.readedXElement.getSingleTextNotNull("Remark");
                                if(issueTypeIntValue==11){
                                    //保证人地址
                                    detail.guarAddr=remark;
                                }else if(issueTypeIntValue==14 || issueTypeIntValue==15){
                                    //拒付理由
                                    detail.unPayMemo=remark;
                                }
                                retAts.writeDetail(detail);
                            }
                        } else {
                            respCode = "-1";
                            respInfo = StringHelper.isNullOrEmpty(IBSReturnMsg)?"银企直联通讯机校验错误，或者通讯失败！":IBSReturnMsg;
                        }
                    } else {
                        respCode = "-1";
                        respInfo = tmpSbMsg.toString();
                    }
                } else {
                    respCode = "-1";
                    respInfo = tmpStrRet[1];
                }
            } catch (Exception e) {
                respCode = "-1";
                respInfo = e.getMessage();
                WriteBankLogLn("查询背面信息异常："+e.getMessage());
            }
        }
        if("-1".equals(respCode)){
            retAts = new RetAtsB005BillBackQry(query.reqHeader);
            retAts.writeStart();
            retAts.writeRespCode(respCode);
            retAts.writeRespCode(respInfo);
        }
        retAts.writeEnd();
        return retAts;
    }

    //结果查询 ok
    @Override
    public RetAtsB006BillStateQry D_B006_BillState_Qeury(ReqB006BillStateQry query) throws Exception {
        RetAtsB006BillStateQry retAts = new RetAtsB006BillStateQry(query.reqHeader);
        retAts.writeBeforeDetail();
        while (query.readRecord()) {
            if("1".equals(query.detail.operType) || "4".equals(query.detail.operType)) {
                queryBillByB2EAcceptCancelableQry(query, retAts);
            } else {
                //应答结果查询
                queryBillByB2EEdraftReplyQry(query, retAts);
            }
        }
        retAts.writeEnd();
        return retAts;
    }

    private void queryBillByB2EEdraftReplyQry(ReqB006BillStateQry query, RetAtsB006BillStateQry retAts) throws Exception {
        //原电票交易类型
        String busiType = "";
        //    业务类型 1-出票 2-承兑 3-收票 4-背书转让 5-质押 6-解质押 7-出票保证
        //8-承兑保证 9-背书保证 10-贴现 11-回购式贴现赎回 12-提示付款 13-追索 14-同意清偿
        switch (Integer.valueOf(query.detail.busiType)) {
            case 2: busiType = "0001";break;//ok
            case 3: busiType = "0002";break;//ok
            case 4: busiType = "0003";break;
            case 5: busiType = "0012";break;
            case 6: busiType = "0013";break;
            case 7: busiType = "0011";break;
            case 8: busiType = "0011";break;
            case 9: busiType = "0011";break;
            case 10: busiType = "0004";break;
            case 11: busiType = "0005";break;
            case 12:
                if ("1".equals(query.detail.overdueFlag)) {
                    busiType = "0014";//正常提示付款
                } else if ("2".equals(query.detail.overdueFlag)) {
                    busiType = "0015";//逾期提示付款
                } else{//TODO
                    busiType = "0015";
                }
                break;
            case 13: throw new Exception("暂不支持追索回复");
            case 14: busiType = "0016";break;
            default: throw new Exception("类型异常：" + query.detail.busiType);
        }
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //赋值通用参数
        SDEBANK01Header bankHeader = new SDEBANK01Header();
        bankHeader.TrsCode = "B2EEdraftReplyQry";//银行交易码
        bankHeader.ERPJnlNo = getQryTransID();//生成交易流水号
        bankHeader.ERPTrsTimestamp = query.reqHeader.transTime;//指令请求时间
        //写入SDEBANK01通用的头部节点
        writeCommonHeader(xmlWriter, bankHeader);
        //写入发送给银行报文体
        xmlWriter.writeStartElement("Body");
        xmlWriter.writeElementString("EnterpriseAccount", query.detail.actNum);//账号
        xmlWriter.writeElementString("DraftType", getBillType(query.detail.billType));//AC01银承
        xmlWriter.writeElementString("TransType", busiType);//交易类型
        xmlWriter.writeElementString("DueDtBegin", "");//查询起始日期
        xmlWriter.writeElementString("DueDtEnd", "");//查询结束日期
        String replayType = "";
        if ("2".equals(query.detail.operType)) {
            replayType = "SU00";
        } else if ("3".equals(query.detail.operType)) {
            replayType = "SU01";
        }
        xmlWriter.writeElementString("AccptrReplyMk", replayType);//应答标识
        //7-出票保证 8-承兑保证 9-背书保证
        String ensureType = "";
        if ("7".equals(query.detail.busiType)) {
            ensureType = "0";
        } else if ("8".equals(query.detail.busiType)) {
            ensureType = "1";
        } else if ("9".equals(query.detail.busiType)) {
            ensureType = "2";
        }
        xmlWriter.writeElementString("GuarntType", ensureType);//保证类别
        xmlWriter.writeEndDocument();
        // 发送给银行获得应答报文
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankHeader);
        RetAtsB006BillStateQryDetail detail = new RetAtsB006BillStateQryDetail();

        if (tmpStrRet[0].length() == 0) {// 成功响应
            StringBuilder tmpSbMsg = new StringBuilder();//存提示信息
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);// 把银行返回报文转成XmlTextReader格式
            if (tmpBkReader != null) {// 返回报文的内容不为空
                String IBSReturnCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                String IBSReturnMsg = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                if ("000000".equals(IBSReturnCode)) {
                    while (tmpBkReader.read("/Message/Body/List/Map")) {
                        String memo = tmpBkReader.readedXElement.getSingleText("sgnUpremark");//回复人备注
                        String billNo = tmpBkReader.readedXElement.getSingleText("draftNb");
                        if (StringHelper.hasAnyChar(memo) && memo.contains(query.detail.reqSeqID) && query.detail.billNo.equals(billNo)) {
                            detail.respCode = "0";
                            detail.respInfo = IBSReturnMsg;
                            detail.reqSeqID = query.detail.reqSeqID;
                            detail.billNo = billNo;
                            detail.billState = tmpBkReader.readedXElement.getSingleText("drftStat");
                            detail.transState = tmpBkReader.readedXElement.getSingleText("transRes");
                            detail.payInfoCode = detail.transState;
                            detail.busiNo = tmpBkReader.readedXElement.getSingleText("EcdsDate") + "_" +
                                    tmpBkReader.readedXElement.getSingleText("EcdsSeq");
                            break;
                        }else {
                            detail.respCode = "-1";
                            detail.respInfo = "查无记录，请查看日志";
                        }
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = IBSReturnMsg;
                }
            } else {
                detail.respCode = "-1";
                detail.respInfo = tmpSbMsg.toString();
            }
        } else {
            detail.respCode = "-1";
            detail.respInfo = tmpStrRet[1];
        }
        retAts.writeDetail(detail);
    }

    //追索人查询 ok
    @Override
    public RetAtsB007BillKzsrQry D_B007_BillKzsr_Query(ReqB007BillKzsrQry query) throws Exception {
        RetAtsB007BillKzsrQry retAts = new RetAtsB007BillKzsrQry(query.reqHeader);
        retAts.writeBeforeDetail();
        String respCode = "0";
        String respInfo = "";
        while (query.readRecord()) {
            try {
                XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
                //赋值通用参数
                SDEBANK01Header bankHeader = new SDEBANK01Header();
                bankHeader.TrsCode = "B2ERecoverablePerQry";//银行交易码
                bankHeader.ERPJnlNo = getQryTransID();//生成交易流水号
                bankHeader.ERPTrsTimestamp = query.reqHeader.transTime;//指令请求时间
                //写入SDEBANK01通用的头部节点
                writeCommonHeader(xmlWriter, bankHeader);
                //写入发送给银行报文体
                xmlWriter.writeStartElement("Body");
                xmlWriter.writeElementString("DraftNb", query.detail.billNo);//电子票据号码
                xmlWriter.writeElementString("IsseAmt",query.detail.billAmt);//票据金额
                xmlWriter.writeElementString("RcrsTp",getRecourseType(query.detail.recourseType));//追索类型
                xmlWriter.writeElementString("ApplDt",Format.Date8Format.format(new Date()));//追索通知日期
                xmlWriter.writeElementString("ReqAmt",query.detail.billAmt);//追索金额
                xmlWriter.writeElementString("EnterpriseAccount", query.detail.actNum);//追索人账号
                xmlWriter.writeEndDocument();

                // 发送给银行获得应答报文
                String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankHeader);

                if (tmpStrRet[0].length() == 0) {// 成功响应
                    StringBuilder tmpSbMsg = new StringBuilder();//存提示信息
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);// 把银行返回报文转成XmlTextReader格式
                    if (tmpBkReader != null) {// 返回报文的内容不为空
                        String IBSReturnCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                        String IBSReturnMsg = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                        if ("000000".equals(IBSReturnCode)) {
                            retAts.billNo=tmpBkReader.getFirstText("/Message/Body/DraftNb");
                            retAts.writeRespInfos();
                            while (tmpBkReader.read("/Message/Body/List/Map")){
                                RetAtsB007BillKzsrQryDetail detail=new RetAtsB007BillKzsrQryDetail();
                                //收款人
                                detail.receName=tmpBkReader.readedXElement.getSingleTextNotNull("rcvgNm");
                                //收款人组织机构代码
                                detail.receCode=tmpBkReader.readedXElement.getSingleTextNotNull("rcvgCmonId");
                                //收款人人账号
                                detail.receAct=tmpBkReader.readedXElement.getSingleTextNotNull("rcvgAcctId");
                                //收款人人开户行行名
                                detail.receBnkName="";
                                //收款人开户行行号
                                detail.receBnkCode=tmpBkReader.readedXElement.getSingleTextNotNull("rcvgAcctSvcr");
                                retAts.writeDetail(detail);
                            }
                        } else {
                            respCode = "-1";
                            respInfo = StringHelper.isNullOrEmpty(IBSReturnMsg)?"银企直联通讯机校验错误，或者通讯失败！":IBSReturnMsg;
                        }
                    } else {
                        respCode = "-1";
                        respInfo = tmpSbMsg.toString();
                    }
                } else {
                    respCode = "-1";
                    respInfo = tmpStrRet[1];
                }
            } catch (Exception e) {
                respCode = "-1";
                respInfo = e.getMessage();
                e.printStackTrace();
            }
        }
        if("-1".equals(respCode)){
            retAts.respCode=respCode;
            retAts.respInfo=respInfo;
            retAts.writeRespInfos();
        }
        retAts.writeAfter();
        return retAts;
    }

    //票据操作信息查询
    @Override
    public RetAtsB008HistoricalBillQry D_B008_HistoricalBillQuery(ReqB008HistoricalBillQry query) throws Exception {
        return null;
    }

    @Override
    public RetAtsB009DrawBillQry D_B009_DrawBillQuery(ReqB009DrawBillQry query) throws Exception {
        return null;
    }

    @Override
    public RetAtsB010ProtocolStateQry D_B010_ProtocolStateQuery(ReqB010ProtocolStateQry query) throws Exception {
        return null;
    }

    @Override
    public RetAtsB011TransFlagQry D_B011_TransFlagQuery(ReqB011TransFlagQry query) throws Exception {
        return null;
    }

    //查询申请类的票据
    private void queryBillByApply(ReqB001OperBillQry query, RetAtsB001OperBillQry retAts) throws Exception {
        String billType="";
        switch (Integer.valueOf(query.detail.busiType)) {
            case 1: throw new Exception("不支持查询出票申请类票据");
            case 2: billType="0001";break;//承兑
            case 3: billType="0002";break;//提示收票
            case 4: billType="0004";break;//背书转让
            case 5: billType="0006";break;//质押
            case 6: billType="0007";break;//质押解除
            case 10: billType="0005";break;//贴现
            case 12: billType="0008";break;//提示付款
            case 13:
                if ("1".equals(query.detail.recourseType)) {
                    billType="0009";
                } else {
                    billType="0010";
                }
                break;//追索
            case 14://break;//同意清偿
            default: throw new Exception("未知的申请操作类型");
        }
        queryBillByB2EAcceptableQry(query, retAts, billType);
    }

    private void queryBillByB2EAcceptableQry(ReqB001OperBillQry query, RetAtsB001OperBillQry retAts, String billType) throws Exception {
        //赋值通用参数
        SDEBANK01Header bankHeader = new SDEBANK01Header();
        bankHeader.TrsCode = "B2EAcceptableQry";//银行交易码
        bankHeader.ERPTrsTimestamp = query.reqHeader.transTime;//指令请求时间
        bankHeader.ERPJnlNo = getQryTransID();//生成交易流水号
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //写入SDEBANK01通用的头部节点
        writeCommonHeader(xmlWriter, bankHeader);
        //写入发送给银行报文体
        xmlWriter.writeStartElement("Body");
        xmlWriter.writeElementString("EnterpriseAccount", query.detail.actNum);//账号
        xmlWriter.writeElementString("DraftType", getBillType(query.detail.billType));//AC01银承
        xmlWriter.writeElementString("QueryTp", billType);//交易类型
        xmlWriter.writeElementString("DrwrNm", "");//出票人名称
        xmlWriter.writeElementString("DrftStat", "");//票据状态
        xmlWriter.writeElementString("DueDtBegin", query.detail.dueDateUp);//票据到期日开始
        xmlWriter.writeElementString("DueDtEnd", query.detail.dueDateDown);//票据到期日结束
        xmlWriter.writeElementString("IsseAmtLower", query.detail.billAmtDown);//票据金额开始
        xmlWriter.writeElementString("IsseAmtLimit", query.detail.billAmtUp);//票据金额结束
        xmlWriter.writeElementString("AccptrNmSvcr", "");//承兑银行名称
        xmlWriter.writeElementString("DraftNb", query.detail.billNo);//汇票号码
        xmlWriter.writeElementString("EdraftBatchNo", "");//网银批量出票流水号
        xmlWriter.writeEndDocument();

        // 发送给银行获得应答报文
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankHeader);
        if (tmpStrRet[0].length() == 0) {// 成功响应
            StringBuilder tmpSbMsg = new StringBuilder();//存提示信息
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);// 把银行返回报文转成XmlTextReader格式
            if (tmpBkReader != null) {// 返回报文的内容不为空
                String IBSReturnCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                String IBSReturnMsg = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                if ("000000".equals(IBSReturnCode)) {
                    while (tmpBkReader.read("/Message/Body/List/Map")) {
                        RetAtsB001OperBillQryDetail detail = new RetAtsB001OperBillQryDetail();
                        detail.billNo = tmpBkReader.readedXElement.getSingleText("draftNb");
                        detail.billType = getBillType(tmpBkReader.readedXElement.getSingleText("draftType"));
                        detail.issDate = DateHelper.convertD8toD10(tmpBkReader.readedXElement.getSingleText("isseDt"));
                        detail.dueDate = DateHelper.convertD8toD10(tmpBkReader.readedXElement.getSingleText("dueDt"));
                        detail.billAmt = tmpBkReader.readedXElement.getSingleText("isseAmt");
                        //转让标识
                        detail.transFlag = "1";
                        //承兑无条件支付委托 1-无条件支付 0-非条件支付
                        detail.mustPay = "";
                        //出票人
                        detail.drawName = tmpBkReader.readedXElement.getSingleText("drwrNm");
                        detail.drawAct = tmpBkReader.readedXElement.getSingleText("drwrAcctId");
                        //承兑人开户行
                        detail.accpName = tmpBkReader.readedXElement.getSingleText("accptrNm");
                        detail.accpAct = tmpBkReader.readedXElement.getSingleText("accptrId");
                        detail.accpBnkCode = tmpBkReader.readedXElement.getSingleText("accptrSvcr");
                        //<BkFlag1>票据状态</BkFlag1>
                        detail.billStatus = tmpBkReader.readedXElement.getSingleText("drftStat");
                        //收票人名称
                        detail.payeeName = tmpBkReader.readedXElement.getSingleText("rqstrNm");
                        //收票人账号
                        detail.payeeAct = tmpBkReader.readedXElement.getSingleText("rqstrAcct");
                        //开户行行号
                        detail.payeeBnkCode = tmpBkReader.readedXElement.getSingleText("rqstrAcctSvcr");
                        //开户行名称
                        detail.payeeBnkName = tmpBkReader.readedXElement.getSingleText("rqstrNmSvcr");
                        //<BkDetail3>备注2</BkDetail3>
                        String memo=tmpBkReader.readedXElement.getSingleText("draftReRemrk");
                        if(memo.contains("#*#")){
                            detail.memo = memo.substring(0,memo.indexOf("#*#"));
                        }else {
                            detail.memo = memo;
                        }
                        retAts.writeDetail(detail);
                    }
                } else if("999999".equals(IBSReturnCode) && "无结果查询的记录".equals(IBSReturnMsg)){
                    retAts.respCode = "0";
                    retAts.respInfo = IBSReturnMsg.trim();
                }else {
                    retAts.respCode = "-1";
                    retAts.respInfo = StringHelper.isNullOrEmpty(IBSReturnMsg)?"银企直联通讯机校验错误，或者通讯失败！":IBSReturnMsg;
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpSbMsg.toString();
            }
        } else {
            retAts.respCode = "-1";
            retAts.respInfo = tmpStrRet[1];
        }
    }

    private void queryBillByB2EAcceptCancelableQry(ReqB006BillStateQry query, RetAtsB006BillStateQry retAts) throws Exception {
        XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
        //赋值通用参数
        SDEBANK01Header bankHeader = new SDEBANK01Header();
        bankHeader.TrsCode = "B2EAcceptCancelableQry";//银行交易码
        bankHeader.ERPJnlNo = getQryTransID();//生成交易流水号
        bankHeader.ERPTrsTimestamp = query.reqHeader.transTime;//指令请求时间
        //写入SDEBANK01通用的头部节点
        writeCommonHeader(xmlWriter, bankHeader);
        //写入发送给银行报文体
        xmlWriter.writeStartElement("Body");
        xmlWriter.writeElementString("EnterpriseAccount", query.detail.actNum);//账号
        xmlWriter.writeElementString("DraftType", getBillType(query.detail.billType));//AC01银承
        if("1".equals(query.detail.operType) && "1".equals(query.detail.busiType)){
            Connection conn = FGBiz.getLocalDbConn();
            try {
                Map<String, Object> selMap = JDBCHelper.execQuerySingle(conn, SELECTSEQONE, new Object[]{query.reqHeader.reqSeqID, query.reqHeader.bankCode});
                if (selMap.size() > 0) {
                    query.detail.billNo = selMap.get("BANKREQSEQID").toString();
                }else {
                    query.detail.billNo="";
                }
            } catch (Exception ex) {
                WriteBankLogLn("查询票据号失败");
                query.detail.billNo="";
            } finally {
                if (conn != null) {
                    conn.close();
                }
            }
        }
        xmlWriter.writeElementString("DraftNb", query.detail.billNo);//电子票据号码
        xmlWriter.writeElementString("DrftStat", "");//票据状态
        xmlWriter.writeElementString("QueryTp", getBusiType(query.detail.operType,query.detail.busiType));//电票交易类型
        xmlWriter.writeElementString("IsseDtBegin", "");//查询起始日期
        xmlWriter.writeElementString("IsseDtEnd", "");//查询结束日期
        xmlWriter.writeElementString("IsseAmtLower", "");//票据金额1
        xmlWriter.writeElementString("IsseAmtLimit", "");//票据金额2
        xmlWriter.writeElementString("DueDtBegin", "");//票据到期日开始
        xmlWriter.writeElementString("DueDtEnd", "");//票据到期日结束
        xmlWriter.writeEndDocument();

        // 发送给银行获得应答报文
        String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), bankHeader);
        RetAtsB006BillStateQryDetail detail = new RetAtsB006BillStateQryDetail();

        if (tmpStrRet[0].length() == 0) {// 成功响应
            StringBuilder tmpSbMsg = new StringBuilder();//存提示信息
            XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);// 把银行返回报文转成XmlTextReader格式
            if (tmpBkReader != null) {// 返回报文的内容不为空
                String IBSReturnCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                String IBSReturnMsg = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                if ("000000".equals(IBSReturnCode)) {
                    int readerNum=tmpBkReader.countElement("/Message/Body/List/Map");
                    if(readerNum==1){//撤票的时候无法埋入流水号
                        while (tmpBkReader.read("/Message/Body/List/Map")) {
                            String billNo = tmpBkReader.readedXElement.getSingleText("draftNb");
                            if (query.detail.billNo.equals(billNo)) {
                                detail.respCode = "0";
                                detail.respInfo = tmpBkReader.readedXElement.getSingleText("errorInfo");
                                detail.reqSeqID = query.detail.reqSeqID;
                                detail.billState = tmpBkReader.readedXElement.getSingleText("drftStat");
                                detail.billNo = billNo;
                                detail.transState = tmpBkReader.readedXElement.getSingleText("TransResult");
                                detail.payInfoCode = detail.transState;
                                detail.busiNo = tmpBkReader.readedXElement.getSingleText("EcdsDate") + "_" +
                                        tmpBkReader.readedXElement.getSingleText("EcdsSeq");
                                break;
                            } else {
                                detail.respCode = "-1";
                                detail.respInfo = "查无记录";
                            }
                        }
                    }else {
                        while (tmpBkReader.read("/Message/Body/List/Map")) {
                            String memo = tmpBkReader.readedXElement.getSingleText("draftReRemrk");
                            String billNo = tmpBkReader.readedXElement.getSingleText("draftNb");
                            if (StringHelper.hasAnyChar(memo) && memo.contains(query.detail.reqSeqID) && query.detail.billNo.equals(billNo)) {
                                detail.respCode = "0";
                                detail.respInfo = tmpBkReader.readedXElement.getSingleText("errorInfo");
                                detail.reqSeqID = query.detail.reqSeqID;
                                detail.billState = tmpBkReader.readedXElement.getSingleText("drftStat");
                                detail.billNo = billNo;
                                detail.transState = tmpBkReader.readedXElement.getSingleText("TransResult");
                                detail.payInfoCode = detail.transState;
                                detail.busiNo = tmpBkReader.readedXElement.getSingleText("EcdsDate") + "_" +
                                        tmpBkReader.readedXElement.getSingleText("EcdsSeq");
                            } else {
                                detail.respCode = "-1";
                                detail.respInfo = "查无记录，请查看日志";
                            }
                        }
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = IBSReturnMsg;
                }
            } else {
                detail.respCode = "-1";
                detail.respInfo = tmpSbMsg.toString();
            }
        } else {
            detail.respCode = "-1";
            detail.respInfo = tmpStrRet[1];
        }
        retAts.writeDetail(detail);
    }
}
